

import { createFormRule, FormRuleProperty } from 'legions-decorator/async.validator'
import { HlLabeledValue,BaseFormFields, IBaseFormFields } from 'legions-lunar/model';
import get from 'lodash/get'
import set from 'lodash/set'
import has from 'lodash/has'
import camelCase from 'lodash/camelCase';
import TableQueryParamsProperty from '../../containers/components.property/tableQueryParamsProperty';
import { LegionsProForm } from 'legions-pro-design';
/** 搜索条件表单Model */
export class PropertyQueryFormFields extends LegionsProForm.ProFormFields<PropertyQueryFormFields> {
	@FormRuleProperty({
		required: true,
		name: 'label',
		error: 'Label名称',
		desc: 'Label名称',
		type: 'string',
	})
	label: IBaseFormFields<string> = {
		value: '',
		requestParamKey: 'label',
		submitBeforeTransform: (value) => {
			return value
		},
	}
	
	@FormRuleProperty({
		required: true,
		name: 'componentName',
		error: '组件属性名称只能为英文字符',
		desc: '组件属性名称',
		type: 'string',
        validator: (value: string,error,callback,props: { that:TableQueryParamsProperty}) => {
            const ViewCanvasApp = props.that.props.store.context.listPagePropertyApp.context.QueryPropertyApp;
            let reg = /^[A-Z a-z]+$/
            if (!reg.test(value)) {
                callback(error)
            }
            const index = ViewCanvasApp.queryComponentRef.viewModel.computedQuery.findIndex((item) => item.containerProps.name === value)
			if (index > -1) {
                const uuid = ViewCanvasApp.queryComponentRef.viewModel.computedQuery[index].containerProps.uuid;
                if (uuid !== ViewCanvasApp.editUid) {
                    callback('组件属性名称存在相同数据，请重新输入')
				} 
				else if (camelCase(value) !== value) {
					callback('组件属性名称只能为驼峰形式，请重新输入')
				}
				else {
                    callback()
                }
			}
			else if (camelCase(value) !== value) {
				callback('组件属性名称只能为驼峰形式，请重新输入')
			}
            else {
                callback()
            }
        }
	})
	componentName: IBaseFormFields<string> = {
        value: '',
        requestParamKey: 'name',
	}
	@FormRuleProperty({
		required: true,
		name: 'containermd',
		error: '总宽度',
		desc: '总宽度',
		type: 'number',
	})
	containermd: IBaseFormFields<string> = {
		value: void 0,
	}
	@FormRuleProperty({
		required: true,
		name: 'containerlg',
		error: '总宽度',
		desc: '总宽度',
		type: 'number',
	})
	containerlg: IBaseFormFields<string> = {
		value: void 0,
	}
	@FormRuleProperty({
		required: true,
		name: 'containerxl',
		error: '总宽度',
		desc: '总宽度',
		type: 'number',
	})
	containerxl: IBaseFormFields<string> = {
		value: void 0,
	}
	@FormRuleProperty({
		required: false,
		name: 'placeholder',
		error: '占位字符',
		desc: '占位字符',
		type: 'string',
	})
	placeholder: IBaseFormFields<string> = {
        value: void 0,
        requestParamKey: 'placeholder',
	}

    @FormRuleProperty({
		required: true,
		name: 'labelSpan',
		error: 'label宽度',
		desc: 'label宽度',
		type: 'number',
	})
	labelSpan: IBaseFormFields<string> = {
		value: void 0,
		requestParamKey: 'labelSpan',
		submitBeforeTransform: (value) => {
			return value
		},
	}
	@FormRuleProperty({
		required: true,
		name: 'jsonProperty',
		error: '接口字段名称只能为英文字符',
		desc: '接口字段名称',
        type: 'string',
        validator: (value: string,error,callback,props: { that:TableQueryParamsProperty; }) => {
            const ViewCanvasApp = props.that.props.store.context.listPagePropertyApp.context.QueryPropertyApp;
            console.log(props)
            let reg = /^[A-Z a-z]+$/
            if (!reg.test(value)) {
                callback(error)
            }
            const index = ViewCanvasApp.queryComponentRef.viewModel.computedQuery.findIndex((item) => item['jsonProperty'] === value)
            if (index > -1) {
                const uuid = ViewCanvasApp.queryComponentRef.viewModel.computedQuery[index].containerProps.uuid;
                if (uuid !== ViewCanvasApp.editUid) {
                    callback('接口字段名称存在相同数据，请重新输入')
                } else {
                    callback()
                }
                
            }
            else {
                callback()
            }
        }
	})
	jsonProperty: IBaseFormFields<string> = {
		value: void 0,
		requestParamKey:'jsonProperty',
		submitBeforeTransform: (value) => {
			return value
		},
	}
	@FormRuleProperty({
		required: false,
		name: 'maxlength',
		error: '字符长度',
		desc: '字符长度',
		type: 'string',
	})
	maxlength: IBaseFormFields<string> = {
		value: void 0,
		submitBeforeTransform: (value) => {
			return value 
		},
	}
    @FormRuleProperty({
        required: false,
        name: 'selectOptionsValue',
        error: '数据项',
        desc: '数据项',
        type: 'object',
    })
    selectOptionsValue: IBaseFormFields<{ key: string; label: string;title: string }> = {
        value: undefined,
        requestParamKey: 'selectOptionsValue',
        submitBeforeTransform: (value) => {
            return value?value.key:''
        },
    }
	constructor(form?: PropertyQueryFormFields) {
		super()
		PropertyQueryFormFields.initMapPropsToFields.call(this, form)
	}
}

export class PropertyQuerySearchFields extends LegionsProForm.ProFormFields<PropertyQuerySearchFields>{
	@FormRuleProperty({
		required: false,
		name: 'onQuerySearch',
		error: '搜索函数',
		desc: '搜索函数',
		type: 'string',
	})
	onQuerySearch: IBaseFormFields<string> = {
		value: void 0,
		requestParamKey: 'onSearch',
		submitBeforeTransform: (value) => {
			return value
		},
	}
	@FormRuleProperty({
		required: false,
		name: 'onReset',
		error: '重置函数',
		desc: '重置函数',
		type: 'string',
	})
	onQueryReset: IBaseFormFields<string> = {
		value: void 0,
		requestParamKey: 'onReset',
		submitBeforeTransform: (value) => {
			return value
		},
	}
	@FormRuleProperty({
		required: false,
		name: 'onRefresh',
		error: '刷新函数',
		desc: '刷新函数',
		type: 'string',
	})
	onRefresh: IBaseFormFields<string> = {
		value: void 0,
		requestParamKey: 'onRefresh',
		submitBeforeTransform: (value) => {
			return value
		},
	}
	constructor(form?: PropertyQuerySearchFields) {
		super()
		PropertyQuerySearchFields.initMapPropsToFields.call(this, form)
	}
}







